<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01星球</title>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        /* 保持原有样式不变 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
            background-color: #fff;
            color: #333;
            line-height: 1.6;
        }

        /* 导航栏样式 */
        .top-nav {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            background-color: #165DFF;
            color: #fff;
            padding: 15px 5%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            z-index: 999;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .nav-left {
            display: flex;
            align-items: center;
        }

        .logo {
            font-size: 24px;
            font-weight: bold;
            margin-right: 30px;
            letter-spacing: 0.5px;
        }

        .nav-links {
            display: flex;
        }

        .nav-links a {
            color: #fff;
            text-decoration: none;
            margin-right: 25px;
            font-size: 16px;
            padding: 5px 0;
            position: relative;
            transition: all 0.3s ease;
        }

        .nav-links a:hover {
            opacity: 0.9;
        }

        .nav-links a:after {
            content: '';
            position: absolute;
            width: 0;
            height: 2px;
            bottom: 0;
            left: 0;
            background-color: #fff;
            transition: width 0.3s ease;
        }

        .nav-links a:hover:after {
            width: 100%;
        }

        .nav-right {
            display: flex;
            align-items: center;
        }

        .search-box {
            margin-right: 25px;
            position: relative;
        }

        .search-box input {
            padding: 8px 15px 8px 35px;
            border: none;
            border-radius: 20px;
            width: 200px;
            transition: all 0.3s ease;
            font-size: 14px;
        }

        .search-box input:focus {
            width: 250px;
            outline: none;
            box-shadow: 0 0 0 2px rgba(255,255,255,0.3);
        }

        .search-box i {
            position: absolute;
            left: 12px;
            top: 50%;
            transform: translateY(-50%);
            color: #888;
        }

        .avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background-color: #f0f7ff;
            margin-right: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #165DFF;
            font-weight: bold;
            font-size: 16px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            cursor: pointer;
        }

        .login-btn a {
            color: #165DFF;
            text-decoration: none;
            background-color: #fff;
            padding: 8px 18px;
            border-radius: 20px;
            font-weight: 500;
            transition: all 0.3s ease;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .login-btn a:hover {
            background-color: #f0f7ff;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0,0,0,0.15);
        }

        /* 轮播图通用样式 */
        .carousel {
            position: relative;
            overflow: hidden;
        }

        .carousel-container {
            display: flex;
            transition: transform 0.5s ease;
        }

        .carousel-slide {
            min-width: 100%;
        }

        .carousel-slide img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .carousel-dots {
            position: absolute;
            display: flex;
            gap: 10px;
        }

        .carousel-dot {
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .carousel-dot.active {
            background-color: #fff;
        }

        /* 首页轮播图样式 */
        .banner {
            width: 100%;
            margin-top: 66px;
            height: 500px;
        }

        .banner .carousel-dots {
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
        }

        .banner .carousel-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background-color: rgba(255,255,255,0.6);
        }

        .banner .carousel-dot.active {
            width: 30px;
            border-radius: 6px;
        }

        /* 图片展示框样式 */
        .img-section {
            padding: 60px 5%;
            background-color: #f9fbff;
        }

        .section-title {
            text-align: center;
            margin-bottom: 40px;
            color: #165DFF;
            font-size: 28px;
            font-weight: 600;
            position: relative;
        }

        .section-title:after {
            content: '';
            display: block;
            width: 60px;
            height: 3px;
            background-color: #165DFF;
            margin: 10px auto;
            border-radius: 3px;
        }

        .img-show {
            display: flex;
            justify-content: center;
            gap: 30px;
            flex-wrap: wrap;
        }

        .img-item {
            width: 45%;
            min-width: 300px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0,0,0,0.08);
            transition: all 0.3s ease;
        }

        .img-item:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.12);
        }

        .img-item img {
            width: 100%;
            height: 300px;
            object-fit: cover;
            display: block;
        }

        /* 新闻列表样式 */
        .news-section {
            padding: 60px 5%;
        }

        .news-list {
            display: flex;
            gap: 30px;
            flex-wrap: wrap;
        }

        .news-left {
            width: 60%;
            min-width: 300px;
        }

        .news-item {
            padding: 15px 0;
            border-bottom: 1px solid #eee;
            display: flex;
            align-items: center;
            transition: all 0.3s ease;
        }

        .news-item:hover {
            padding-left: 10px;
        }

        .news-date {
            color: #165DFF;
            font-weight: 500;
            margin-right: 15px;
            min-width: 80px;
        }

        .news-title {
            color: #333;
            text-decoration: none;
            transition: color 0.3s ease;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            flex: 1;
        }

        .news-title:hover {
            color: #165DFF;
        }

        /* 右侧新闻轮播样式 */
        .news-right {
            width: 35%;
            min-width: 300px;
            height: 400px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0,0,0,0.08);
        }

        .news-right .carousel-dots {
            bottom: 15px;
            left: 50%;
            transform: translateX(-50%);
            gap: 8px;
        }

        .news-right .carousel-dot {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: rgba(255,255,255,0.6);
        }

        /* 资源列表样式 */
        .resource-section {
            padding: 60px 5%;
            background-color: #f9fbff;
        }

        .resource-list {
            overflow-x: auto;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            border-radius: 10px;
        }

        .resource-list table {
            width: 100%;
            min-width: 800px;
            border-collapse: collapse;
            background-color: #fff;
        }

        .resource-list th, .resource-list td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        .resource-list th {
            background-color: #165DFF;
            color: #fff;
            font-weight: 500;
        }

        .resource-list tr:hover {
            background-color: #f0f7ff;
        }

        .download-btn {
            display: inline-flex;
            align-items: center;
            gap: 5px;
            background-color: #165DFF;
            color: #fff;
            padding: 6px 12px;
            text-decoration: none;
            border-radius: 4px;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .download-btn:hover {
            background-color: #0e47cc;
            transform: translateY(-2px);
        }

        /* 页脚样式 */
        .footer {
            background-color: #165DFF;
            color: #fff;
            text-align: center;
            padding: 30px 5%;
            margin-top: 40px;
        }

        /* 加载和错误提示样式 */
        .loading {
            text-align: center;
            padding: 50px 0;
            color: #165DFF;
            font-size: 18px;
        }

        .loading i {
            margin-right: 8px;
            animation: spin 1.5s linear infinite;
        }

        .error {
            text-align: center;
            padding: 50px 0;
            color: #ff4d4f;
            font-size: 16px;
        }

        .error button {
            margin-top: 15px;
            padding: 8px 16px;
            background-color: #165DFF;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .error button:hover {
            background-color: #0e47cc;
        }

        @keyframes spin {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }

        /* 平滑滚动 */
        html {
            scroll-behavior: smooth;
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .nav-links a {
                margin-right: 15px;
                font-size: 14px;
            }

            .search-box input {
                width: 150px;
            }

            .search-box input:focus {
                width: 180px;
            }

            .banner {
                height: 300px;
            }

            .img-item {
                width: 100%;
            }

            .news-left, .news-right {
                width: 100%;
            }

            .news-right {
                height: 300px;
            }
        }
    </style>
</head>

<body>
<!-- 顶部导航栏 -->
<div class="top-nav">
    <div class="nav-left">
        <div class="logo">01星球</div>
        <div class="nav-links">
            <a href="#home">首页</a>
            <a href="#news">新闻列表</a>
            <a href="#resources">资源下载</a>
            <a href="#contact">联系我们</a>
        </div>
    </div>
    <div class="nav-right">
        <div class="search-box">
            <i class="fa fa-search"></i>
            <input type="text" placeholder="搜索...">
        </div>
        <div class="avatar" id="userAvatar">U</div>
        <div class="login-btn" id="loginBtnGroup">
            <a href="./login.html">登录</a>
        </div>
    </div>
</div>

<!-- 首页轮播图 -->
<div id="home" class="banner carousel">
    <div class="carousel-container">
        <div class="carousel-slide">
            <img src="../images/banner1.png" alt="01星球首页轮播图1">
        </div>
        <div class="carousel-slide">
            <img src="../images/banner2.png" alt="01星球首页轮播图2">
        </div>
        <div class="carousel-slide">
            <img src="../images/banner3.png" alt="01星球首页轮播图3">
        </div>
    </div>
    <div class="carousel-dots">
        <div class="carousel-dot active" data-index="0"></div>
        <div class="carousel-dot" data-index="1"></div>
        <div class="carousel-dot" data-index="2"></div>
    </div>
</div>

<!-- 图片展示框 -->
<div class="img-section">
    <h2 class="section-title">精选内容</h2>
    <div class="img-show">
        <div class="img-item">
            <img src="../images/banner4.png" alt="精选图片1">
        </div>
        <div class="img-item">
            <img src="../images/banner5.png" alt="精选图片2">
        </div>
    </div>
</div>

<!-- 新闻列表 -->
<div id="news" class="news-section">
    <h2 class="section-title">最新资讯</h2>
    <div class="news-list">
        <div class="news-left" id="newsListContainer">
            <!-- 新闻列表将通过fetch动态渲染 -->
            <div class="loading">
                <i class="fa fa-spinner"></i>加载中...
            </div>
        </div>
        <div class="news-right carousel">
            <div class="carousel-container">
                <div class="carousel-slide">
                    <img src="../images/banner6.png" alt="新闻轮播图1">
                </div>
                <div class="carousel-slide">
                    <img src="../images/banner7.png" alt="新闻轮播图2">
                </div>
                <div class="carousel-slide">
                    <img src="../images/banner8.png" alt="新闻轮播图3">
                </div>
            </div>
            <div class="carousel-dots">
                <div class="carousel-dot active"></div>
                <div class="carousel-dot"></div>
                <div class="carousel-dot"></div>
            </div>
        </div>
    </div>
</div>

<!-- 资源列表 -->
<div id="resources" class="resource-section">
    <h2 class="section-title">资源下载</h2>
    <div class="resource-list" id="resourceListContainer">
        <!-- 资源列表将通过fetch动态渲染 -->
        <div class="loading">
            <i class="fa fa-spinner"></i>加载中...
        </div>
    </div>
</div>

<!-- 页脚 -->
<div id="contact" class="footer">
    <div style="text-align: center; padding: 20px; max-width: 800px; margin: 0 auto;">
        <p style="margin-bottom: 15px; font-size: 16px;">联系我们：1016767724</p>
    </div>
    <p>© 2025 01星球 版权所有</p>
    <p style="margin-top: 10px; font-size: 14px; opacity: 0.8;">逸风组 技术支持</p>
</div>

<script>
    // 封装轮播图通用函数
    function initCarousel(carouselSelector, interval = 5000) {
        const carousel = document.querySelector(carouselSelector);
        if (!carousel) return;

        const container = carousel.querySelector('.carousel-container');
        const slides = carousel.querySelectorAll('.carousel-slide');
        const dots = carousel.querySelectorAll('.carousel-dot');
        let currentIndex = 0;

        // 显示指定索引的幻灯片
        function showSlide(index) {
            if (index >= slides.length) currentIndex = 0;
            if (index < 0) currentIndex = slides.length - 1;

            container.style.transform = `translateX(-${currentIndex * 100}%)`;

            dots.forEach((dot, i) => {
                dot.classList.toggle('active', i === currentIndex);
            });
        }

        // 点击指示器切换幻灯片
        dots.forEach((dot, i) => {
            dot.addEventListener('click', () => {
                currentIndex = i;
                showSlide(currentIndex);
            });
        });

        // 自动轮播
        setInterval(() => {
            currentIndex++;
            showSlide(currentIndex);
        }, interval);
    }

    // 初始化轮播图
    initCarousel('.banner');
    initCarousel('.news-right', 3000);

    // 加载新闻列表
    async function loadNews() {
        const container = document.getElementById('newsListContainer');

        try {
            // 发起请求获取新闻数据
            const response = await fetch('/news');

            if (!response.ok) {
                throw new Error(`HTTP错误，状态码: ${response.status}`);
            }

            const result = await response.json();

            // 检查数据是否有效（接口返回格式为{code, message, data: []}）
            if (result.code !== 200 || !Array.isArray(result.data)) {
                throw new Error('获取的新闻数据格式不正确');
            }

            const newsData = result.data;

            // 清空加载状态并渲染新闻列表
            container.innerHTML = '';

            if (newsData.length === 0) {
                container.innerHTML = '<div class="error">暂无新闻数据</div>';
                return;
            }

            // 遍历新闻数据并生成HTML
            newsData.forEach(news => {
                // 格式化日期显示（使用接口返回的publishTime字段）
                const date = new Date(news.publishTime);
                const formattedDate = `${date.getMonth() + 1}-${date.getDate()}`;

                const newsItem = document.createElement('div');
                newsItem.className = 'news-item';
                newsItem.innerHTML = `
                    <span class="news-date">${formattedDate}</span>
                    <a href="./newsDetails.html?id=${news.newsId}" class="news-title" title="${news.title}">${news.title}</a>
                `;
                container.appendChild(newsItem);
            });

        } catch (error) {
            console.error('加载新闻失败:', error);
            container.innerHTML = `
                <div class="error">
                    加载新闻失败: ${error.message}
                    <button onclick="loadNews()">重试</button>
                </div>
            `;
        }
    }

    // 加载资源列表
    async function loadResources() {
        const container = document.getElementById('resourceListContainer');

        try {
            // 发起请求获取资源数据
            const response = await fetch('/resources');

            if (!response.ok) {
                throw new Error(`HTTP错误，状态码: ${response.status}`);
            }

            const result = await response.json();

            // 检查数据是否有效（接口返回格式为{code, message, data: []}）
            if (result.code !== 200 || !Array.isArray(result.data)) {
                throw new Error('获取的资源数据格式不正确');
            }

            const resourcesData = result.data;

            // 清空加载状态并渲染资源列表
            container.innerHTML = '';

            if (resourcesData.length === 0) {
                container.innerHTML = '<div class="error">暂无资源数据</div>';
                return;
            }

            // 创建表格并添加数据
            let tableHtml = `
                <table>
                    <thead>
                        <tr>
                            <th>序号</th>
                            <th>名称</th>
                            <th>类型</th>
                            <th>时间</th>
                            <th>上传者</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
            `;

            resourcesData.forEach((resource, index) => {
                // 格式化日期为YYYY-MM-DD（使用接口返回的uploadTime字段）
                const date = new Date(resource.uploadTime);
                const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;

                tableHtml += `
                    <tr>
                        <td>${index + 1}</td>
                        <td>${resource.fileName}</td>
                        <td>${resource.type}</td>
                        <td>${formattedDate}</td>
                        <td>${resource.uploader}</td>
                        <td><a href="/resources/${resource.resourceId}/download" class="download-btn" download><i class="fa fa-download"></i> 下载</a></td>
                    </tr>
                `;
            });

            tableHtml += `
                    </tbody>
                </table>
            `;

            container.innerHTML = tableHtml;

        } catch (error) {
            console.error('加载资源失败:', error);
            container.innerHTML = `
                <div class="error">
                    加载资源失败: ${error.message}
                    <button onclick="loadResources()">重试</button>
                </div>
            `;
        }
    }

    // 检查用户登录状态
    function checkLoginStatus() {
        const token = localStorage.getItem('token');
        const avatar = document.getElementById('userAvatar');
        const loginBtnGroup = document.getElementById('loginBtnGroup');

        if (token) {
            // 如果有token，显示用户名首字母并修改登录按钮为退出登录
            const username = localStorage.getItem('username') || 'U';
            avatar.textContent = username.charAt(0).toUpperCase();

            loginBtnGroup.innerHTML = `
                <a href="javascript:void(0)" id="logoutBtn">退出登录</a>
            `;

            // 添加退出登录事件
            document.getElementById('logoutBtn').addEventListener('click', () => {
                localStorage.removeItem('token');
                localStorage.removeItem('username');
                location.reload();
            });
        }
    }

    // 页面加载完成后执行
    document.addEventListener('DOMContentLoaded', () => {
        // 加载新闻和资源数据
        loadNews();
        loadResources();

        // 检查登录状态
        checkLoginStatus();
    });
</script>
</body>
</html>